#include "ComponentAnimations.h"

namespace CompAnims
{

	Move::Move (Component* c, int x1, int y1, int x2, int y2, int durationMs)
		:	LinearXYAnimation (c,String::empty,T("move"),x1,y1,x2,y2,durationMs),
			ComponentDeletionWatcher (c)
	{
	}

	Move::~Move ()
	{
	}

	void Move::applyUpdateToTarget (int x, int y)
	{
		Component* c = (Component*) getComponent ();
		if (c != 0)
		{
			c->setTopLeftPosition (x,y);
		}
	}

	//---------------------------------
	Size::Size (Component* c, int w1, int h1, int w2, int h2, int durationMs)
		:	LinearXYAnimation (c,String::empty,T("size"),w1,h1,w2,h2,durationMs),
			ComponentDeletionWatcher (c)
	{
	}

	Size::~Size ()
	{
	}

	void Size::applyUpdateToTarget (int x, int y)
	{
		Component* c = (Component*) getComponent ();
		if (c != 0)
		{
			c->setSize (x,y);
		}
	}

	//---------------------------------
	ColourChange::ColourChange (Component* c, int colourId, const Colour& c1, const Colour& c2, int durationMs)
		:	LinearColourAnimation (c,String(colourId),T("compcolour"),c1,c2,durationMs),
			ComponentDeletionWatcher (c),
			id (colourId)
	{
		setLooping (false,false,0);
	}

	ColourChange::~ColourChange ()
	{
	}

	void ColourChange::applyUpdateToTarget (const Colour& colour)
	{
		Component* c = (Component*) getComponent ();
		if (c != 0)
		{
			c->setColour (id, colour);
		}
	}

	//---------------------------------
	DoubleProperty::DoubleProperty (Component* c, const String& propName, const double start, const double end, int durationMs)
		:	LinearDoubleAnimation (c,propName,T("compprop"),start,end,durationMs),
			ComponentDeletionWatcher (c),
			propertyName (propName)
	{
		setLooping (false,false,1);
	}

	DoubleProperty::~DoubleProperty ()
	{
	}

	void DoubleProperty::applyUpdateToTarget (const double value)
	{
		Component* c = (Component*) getComponent ();
		if (c != 0)
		{
			c->setComponentProperty (propertyName, value);
		}
	}

};

//=============================================================================
SuperComponentAnimator::SuperComponentAnimator ()
{
}

SuperComponentAnimator::~SuperComponentAnimator ()
{
}

void SuperComponentAnimator::changeColour (Component* c, int colourId, const Colour& newColour, int durationMs, bool loop, bool pingPong, int numTimes)
{
	CompAnims::ColourChange* anim = new CompAnims::ColourChange (c, colourId, c->findColour(colourId), newColour, durationMs);
	anim->setLooping (loop, pingPong, numTimes);
	start (anim);
}

void SuperComponentAnimator::changeDoubleProperty (Component* c, const String& propName, double targetValue, int durationMs, bool loop, bool pingPong, int numTimes)
{
	CompAnims::DoubleProperty* anim = new CompAnims::DoubleProperty (c, propName, c->getComponentPropertyDouble(propName,false), targetValue, durationMs);
	anim->setLooping (loop, pingPong, numTimes);
	start (anim);
}

void SuperComponentAnimator::changePosition (Component* componentToMove, int targetX, int targetY, int durationMs, bool loop, bool pingPong, int numTimes)
{
	CompAnims::Move* m = new CompAnims::Move (	componentToMove,
												componentToMove->getX(),
												componentToMove->getY(),
												targetX,targetY,durationMs	);
	m->setLooping (loop, pingPong, numTimes);
	start (m);
}

void SuperComponentAnimator::changeSize (Component* componentToResize, int targetW, int targetH, int durationMs,bool loop, bool pingPong, int numTimes)
{
	CompAnims::Size* s = new CompAnims::Size (	componentToResize,
												componentToResize->getWidth(),
												componentToResize->getHeight(),
												targetW,targetH,durationMs	);
	s->setLooping (loop, pingPong, numTimes);
	start (s);
}

void SuperComponentAnimator::frameFinished (void* target)
{
	Component* t = (Component*) target;
	if (t) t->repaint ();
}

